package com.Jano.servlet;

import com.Jano.dto.UserDto;
import com.Jano.entity.User;
import com.Jano.exception.BusinessException;
import com.Jano.service.Impl.UserServiceImpl;
import com.Jano.service.UserService;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.Objects;

import static com.Jano.constant.BusinessConstant.SESSION_USER;
import static com.Jano.constant.BusinessConstant.SESSION_VALIDATE_CODE;

/**
 * description: UserServlet <br>
 * date: 2022/6/6 12:14 PM <br>
 * author: zoumaoji <br>
 * version: 1.0 <br>
 */

@WebServlet(urlPatterns = "/users/*")
public class UserServlet extends AbstractBaseServlet {
    UserService userService = new UserServiceImpl();

    @Override
    public Object doService(String action, HttpServletRequest req, HttpServletResponse res) throws UnsupportedEncodingException {
        if ("login".equals(action)) {
            //获取session中的SESSION_VALIDATE_CODE 登录用户对应的属性名
            String corrValidateCode = String.valueOf(req.getSession().getAttribute(SESSION_VALIDATE_CODE));
            //用户填写的验证码
            String validateCode = req.getParameter("validateCode");
            //用户填写的名字
            String userName = req.getParameter("userName");
            //用户填写的密码
            String passWord = req.getParameter("passWord");
            //对比用户输入的验证码和用户填写的验证码是否相等
            if (!StringUtils.equalsIgnoreCase(validateCode, corrValidateCode)) {
                System.out.println("用户填写的是" + validateCode + "系统生成的是" + corrValidateCode);
                throw new BusinessException("验证码错误");
            }
            User userLogin = userService.login(userName, passWord);
            //登陆成功
            //密码需要脱敏
            UserDto userDto = UserDto.builder()
                    .userId(userLogin.getUserId())
                    .userName(userLogin.getUserName())
                    .build();
            //获取session
            HttpSession session = req.getSession();
            //将用户信息存入Session中
            session.setAttribute(SESSION_USER, userDto);
            return userDto;
        }

        //获取当前登录用户
        if ("loginUser".equals(action)) {
            Object sessionUserObj = req.getSession().getAttribute(SESSION_USER);
            //在实际开发中不要将req和session对象传入Service层后去进行一些操作
            // 因为这两个对象是Servlet层对象，不要引起作用域“污染”
            if (Objects.isNull(sessionUserObj)) {
                throw new BusinessException("当前用户尚未登录");
            } else {
                return sessionUserObj;
            }
        }

        //注册操作
        if ("register".equals(action)) {
            String userName = req.getParameter("userName");
            String passWord = req.getParameter("passWord");
            String pwdConfirm = req.getParameter("pwdConfirm");
            userService.register(userName, passWord, pwdConfirm);
        }

        return null;
    }
}
